package cn.kattomy.shop.cart.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import cn.kattomy.shop.cart.vo.Cart;
import cn.kattomy.shop.cart.vo.CartItem;
import cn.kattomy.shop.product.service.ProductService;
import cn.kattomy.shop.product.vo.Product;

import com.google.gson.Gson;
/**
 * 购物车的Action
 * @author kattomy
 *
 */
public class CartAction {

	String result ;
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	//注入商品的 Service
	private ProductService productService;
	
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	//接收商品的 id
	private Integer pid;
	//接受商品的数量
	private Integer count;
	
	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	//增加或减少购物项商品数量的执行方法
	public String addOrSubCart() throws IOException {
		System.out.println(">>>>>>>>>>>>>>>>>");
		//将购物项添加到购物车的执行方法
		//封装一个 CartItem 对象
		CartItem cartItem = new CartItem();
		//获取商品对象
		Product product = productService.findByPid(pid);
		
		//设置数量和商品对象
		cartItem.setCount(count);
		cartItem.setProduct(product);
		
		//将购物项存入购物车中
		//购物车应在 Session 中
		Cart cart = (Cart) ServletActionContext.getRequest().getSession().getAttribute("cart");
		cart.addOrSubCart(cartItem);

		//JSON
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("subTotal", cartItem.getSubtotal());
		resultMap.put("total", cart.getTotal());
//		result.put("subTotal", cartItem.getSubtotal());
//		result.put("total", cart.getTotal());
		
		Gson gson = new Gson();
		result = gson.toJson(resultMap);
//		JSONObject json = JSONObject.fromObject(result); 
//		String result = json.toString();
		
//		System.out.println(">>>>>>>>>>>>>>" + result);
//		ServletActionContext.getResponse().setContentType("/text/javascript");
//		ServletActionContext.getResponse().getWriter().print(jsonStr);
		return "addOrSubCart";
	}
	
	public String addCart() {
		//封装一个 CartItem 对象
		CartItem cartItem = new CartItem();
		//获取商品对象
		Product product = productService.findByPid(pid);
		
		//设置数量和商品对象
		cartItem.setCount(count);
		cartItem.setProduct(product);
		
		//将购物项存入购物车中
		//购物车应在 Session 中
		Cart cart = getCart();
		cart.addCart(cartItem);
		
		return "addCart";
	}

	private Cart getCart() {
		Cart cart = (Cart) ServletActionContext.getRequest().getSession().getAttribute("cart");
		if (cart == null) {
			cart = new Cart();
			ServletActionContext.getRequest().getSession().setAttribute("cart", cart);
		}
		return cart;
	}
	
	/**
	 * 清空购物扯车的执行方法
	 * @return
	 */
	public String clearCart() {
		Cart cart = getCart();
		cart.clearCart();
		return "clearCart";
	}
	
	/**
	 * 移除购物车中指定购物项的执行方法
	 * @return
	 */
	public String removeCart() {
		Cart cart = getCart();
		cart.removeCart(pid);
		return "removeCart";
	}
	
	/**
	 * 转向购物车的执行方法
	 * @return
	 */
	public String myCart() {
		return "myCart";
	}
}
